<?php

namespace app\admin\controller;

use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\Member as MemberModel;
use think\Db;
/**
 * 资讯评论控制器
 * Class NewsTopic
 * @package app\admin\controller
 */
class Newscomment extends Base
{
    /**
     * [getIndex  获取评论]
     * @author [C。]
     * @return json
     */
    public function index()
    {
        $param = input('param.');
        $model = new NewsCommentModel();
        $mmodel = new MemberModel();
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            if(!empty($param['status'])){
                $map['status'] = $param['status'];
            }
            if(!empty($param['start']) || !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            if(!empty($param['parid'])){
                $map['c.parid'] = $param['parid'];
            }
            if(!empty($param['memid'])){
                $map['c.mid'] = $param['memid'];
            }
            if(!empty($param['nid'])){
                $map['n.id'] = $param['nid'];
            }
            $field = 'c.id,n.cover,n.title,m.img,m.username,m.tel,c.content,c.imgs,c.status,c.create_time,c.parid,c.comment_uid';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,$field);
            foreach ($lists as &$v) {
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $v['cover'] = config('domain_name').$v['cover'];
                }
                if(!empty($v['parid'])){
                    $mem = $mmodel->findData($v['comment_uid'],'id','tel,username,img');
                    $v['tel'] = $mem['tel'];
                    $v['img'] = $mem['img'];
                    $v['username'] = $mem['username'];
                }
                $v['content'] = urldecode($v['content']);
                $res = $model->findOne(['parid'=>$v['id']], 'comment_uid,content,create_time,imgs','id','desc');
                if(!empty($res)) {
                    $v['reply'] = urldecode($res['content']);
                    $v['reply_imgs'] = $res['imgs'];
                    $v['reply_user'] = $mmodel->findData($res['comment_uid'],'id','tel,username,img');
                    $v['reply_user']['addtime'] = $res['create_time'];
                }
                $v['reply_num'] = $model->getAllCount(['parid'=>$v['id']]);
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('查看资讯评论列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [edit  禁用评论]
     * @author [C。]
     * @return json
     */
    public function edit()
    {
        $param = input('param.');
        $model = new NewsCommentModel();
        try{
            $comment = $model->findData($param['id'],'id','id,status');
            if($comment['status'] == 1){
                $id = $model->editData(['id'=>$comment['id'],'status'=>2]);
                Db::name('news_comment')->where(['parid'=>$id])->update(['status'=>2]);
            }else{
                $model->editData(['id'=>$param['id'],'status'=>1]);
            }
        }catch(\Exception $e){
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功');
    }
}